<template>
	<view>
		<!-- 		<u-navbar ref="myElement" title="执行操作" :placeholder="true" :autoBack="true" leftIconColor="#FFF"
			bgColor="#267DFF" :titleStyle="{
			color:'#FFF'
		}">
			<view slot="right">
				<image @click="queryShow = true" class="shaixuan" src="/static/img/index/shaixuan.png" mode=""></image>
			</view>
		</u-navbar> -->
		<view class="search">
			<u--input placeholder="请输入票号或操作任务" :clearable="true" prefixIcon="search"
				prefixIconStyle="font-size: 22px;color: #909399" v-model="query.searchValue" @confirm="search"
				@clear="clear">
				<template slot="suffix">
					<view class="search_btn" @click="search"> 搜索</view>
				</template></u--input>
		</view>
		<!-- jumpTo('/pages/detailsPerform/detailsPerform',{
			id:v.id
		})" -->
		<view class="box" v-for=" (v,i) in datas" :key="i" @click="lessVoid(v.id)">
			<view class="box_herder flex align-items justify-content-between">
				<view class="box_herder_left">{{v.orderNumber}}</view>
			</view>
			<view class="box_item">
				<view class="box_label">操作任务：</view>
				<view class="box_value">{{v.taskName}}</view>
			</view>
			<view class="box_item">
				<view class="box_label">操作部门：</view>
				<view class="box_value">{{v.depatName}}</view>
			</view>
			<view class="box_item">
				<view class="box_label">操作人：</view>
				<view class="box_value">{{v.operateUserName}}</view>
			</view>
			<view class="box_item" v-if=" v.startTime && v.endTime">
				<view class="box_label">申请时间：</view>
				<view class="box_value"> {{v.startTime}} <text v-if="v.endTime">至 {{v.endTime}} </text> </view>
			</view>
			<!-- 		<view class="box_item_btn">
				<view class="box_item_btn_left " @click="show = true">关联票 0</view>
				<view class="box_item_btn_right" v-if="i<=3" @click="jumpTo('/pages/addPerform/addPerform')"> 去处理</view>
				<view class="box_item_btn_right" v-else @click="jumpTo('/pages/editPerform/editPerform')"> 交接确认</view>
			</view> -->
		</view>

		<u-empty mode="list" v-if="datas.length<=0" marginTop="80">
		</u-empty>
		<u-popup :show="show" :round="10">
			<view class="block">
				<view class="block_item" v-for=" i in 3" :key="i">
					GGL202301223001 <text :style="{
						color:color.get(i)
					}
					">（待审批）</text>
				</view>
				<view class="hr"></view>
				<view class="block_close" @click="show =false">取消</view>
			</view>
		</u-popup>
		<!-- 查询条件 -->
		<view v-if="queryShow" class="popup" :style="{borderRadius: round + 'rpx', }">
			<view @tap="queryShow = false" class="overlay"></view>
			<view class="content" @tap.stop :style="{
				marginTop:height +'px'
			}">
				<view class="content_time">
					<view class="content_time_title">选择时间</view>
					<view class="content_times margin-top20 flex align-items justify-content-between">
						<view class="content_times_left" @click="dateShow('start')">{{query.startTime || '起始时间'}}</view>
						<view class="content_times_con">-</view>
						<view class="content_times_right" @click="dateShow('end')"> {{query.endTime || '终止时间'}}</view>
					</view>
				</view>
				<view class="content_box flex align-items">
					<view class="content_box_left">
						选择部门
					</view>
					<view class="content_box_cont" @click="treeDpid(0)">{{query.deptName || '全部'}}</view>
					<u-icon size="16" color="#CCCCCC" name="arrow-right"></u-icon>
				</view>
				<view class="content_box flex align-items">
					<view class="content_box_left">
						选择设备
					</view>
					<view class="content_box_cont" @click="treeDpid(1)">{{query.deviceName || '全部'}}</view>
					<u-icon size="16" color="#CCCCCC" name="arrow-right"></u-icon>
				</view>
				<view class="content_box flex align-items" style="align-items: center;">
					<view class="content_box_left">
						票号
					</view>
					<u-input placeholder="请输入票号" v-model="query.id" :fontSize="16"
						placeholderClass="placeholderClass"></u-input>
					<u-icon size="16" color="#CCCCCC" name="arrow-right"></u-icon>
				</view>
				<view class="content_btn flex align-items justify-content-between">
					<view class="content_btn_left" @click="resetting">重置</view>
					<view class="content_btn_right" @click="search">确认</view>

				</view>
			</view>
		</view>
		<u-datetime-picker :show="showDate" v-model="value1" mode="date" @close="showDate = false"
			@cancel="showDate =fasle" @confirm="confirm"></u-datetime-picker>
		<tree ref="tree" @comit="treeComit" :treeData="treeData"></tree>
		<!-- 选择是作废票还是查看详情 -->
		<u-popup :show="optionShow" :round="2" @close="optionShow =false" mode="center">
			<view class="optionShow">
				<view class="optionShow_title">请选择</view>
				<view class="optionShow_box" @click="zfClick(1)">作废</view>
				<view class="optionShow_box" @click="zfClick(2)">详情</view>
			</view>
		</u-popup>

		<u-picker :show="showPick" :columns="options" keyName="dic_key" @cancel="showPick =false" @confirm="voidConfirm"
			title="作废缘由"></u-picker>
	</view>
</template>

<script>
	import {
		lssueList,
		byOperateId,
		getListDeptForZTree,
		deviceInfoList,
		getDicListByCode,
		updateOperateCancel
	} from '@/api/lssue.js'
	const color = new Map([
		[0, "#E04734"],
		[1, "#267DFF"],
		[2, "#8B8B8B"],
		[3, "#E04734"],
	])
	export default {
		data() {
			return {
				color,
				show: false,
				list: [{
						name: '选项一'
					},
					{
						name: '选项二'
					}
				],
				queryShow: false,
				showDate: false,
				value1: Number(new Date()),
				height: 0,
				datas: [],
				round: 10,
				type: '',
				query: {
					startTime: '',
					endTime: '',
					deviceId: '',
					deviceName: '',
					deptId: '',
					deptName: '',
					searchValue: '',
					id: ''
				},
				treeData: [],
				treeDataTwo: [],
				deviceList: [],
				state: '',
				optionShow: false,
				options: [],
				showPick: false,
				void: '',
				voids:'',
				
			};
		},
		watch: {

		},
		onLoad() {
			const token = this.getUrlParams("token");
			if (token) {
				uni.setStorageSync('token', token)
			}
		},
		mounted() {
			this.getElementHeight()
			this.getList()
			this.onSubmitList()
			this.getdeviceInfoList()
			this.getOption()
		},
		methods: {
			lessVoid(v) {
				this.voids = v
				this.void = v
				this.optionShow = true
			},
			zfClick(type){
				this.optionShow = false
				if(type ==1){
					this.showPick = true
				}else if(type ==2){
					this.jumpTo('/pages/detailsPerform/detailsPerform',{id:this.voids})
				}
			},
			async voidConfirm(e) {
				const cancelReason = e.value[0].dic_value
				const res = await updateOperateCancel({
					id: this.void,
					cancelReason: cancelReason
				})
				this.showPick = false
				console.log(e, "e");
				this.$u.toast('操作成功')
				this.getList()

			},
			getUrlParams(name, url) {
				if (!url) url = window.location.href;
				name = name.replace(/[\[\]]/g, '\\$&');
				var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
					results = regex.exec(url);
				if (!results) return null;
				if (!results[2]) return '';
				return decodeURIComponent(results[2].replace(/\+/g, ' '));
			},
			clear() {
				this.query.searchValue = ''
				this.getList()
			},
			search() {
				this.queryShow = false
				this.getList()
			},
			resetting() {
				this.query = {
					startTime: '',
					endTime: '',
					deviceId: '',
					deviceName: '',
					deptId: '',
					deptName: '',
					searchValue: '',
					id: ''
				}
				this.queryShow = false
				this.getList()
			},
			dateShow(type) {
				this.type = type
				this.showDate = true
			},
			treeComit(e) {
				console.log(e, "e");
				if (this.state == 0) {
					this.query.deptName = e[0].label
					this.query.deptId = e[0].id

				} else {
					// this.query.shebei = e[0].label
					this.query.deviceName = e[0].label
					this.query.deviceId = e[0].id
				}

			},
			treeDpid(state) {
				this.state = state
				if (state == 0) {
					this.treeData = this.treeDataTwo
				} else {
					this.treeData = this.deviceList
				}
				this.$refs.tree.itemclick(false, true)
			},
			// 获取设备
			async getdeviceInfoList() {
				const res = await deviceInfoList({
					PAGE_NO: 1,
					PAGE_SIZE: 100000,

				})
				this.deviceList = res.rows.map(v => {
					return {
						id: v.deviceId,
						label: v.deviceName,
						checked: false,
					}
				})
				console.log(res, "res");
			},
			onSubmitList() {
				getListDeptForZTree().then((res) => {
					console.log(res);
					// this.treeData = res.datas.map(ele => this.treeStructure(ele));
					this.treeDataTwo = res.datas.map(ele => this.treeStructure(ele));
					console.log(this.treeData, "treeData");
				});
			},
			treeStructure(ele) {
				const haveChildren = Array.isArray(ele.children) && ele.children.length > 0;
				return {
					id: ele.value,
					label: ele.label,
					checked: false,
					children: haveChildren ? ele.children.map(i => this.treeStructure(i)) : [],
				}
			},
			confirm({
				value
			}) {
				if (this.type == 'start') {
					this.query.startTime = this.formatDate(value)
				} else {
					this.query.endTime = this.formatDate(value)
				}
				this.showDate = false
			},
			async getList() {
				lssueList({
					PAGE_NO: 1,
					PAGE_SIZE: 10000,
					queryType: 3,
					...this.query
				}).then(res => {
					console.log(res, "res");
					this.datas = res.rows
				})
			},
			getElementHeight() {
				uni.createSelectorQuery().in(this).select('.u-navbar').boundingClientRect(data => {
					console.log('Navbar Height:', data.height);
					this.height = data.height
				}).exec();
			},
			formatDate(timestamp) {
				const date = new Date(Number(timestamp));
				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, '0');
				const day = String(date.getDate()).padStart(2, '0');
				return `${year}-${month}-${day}`;
			},
			showPopupAnimation() {

			},
			getOption() {
				getDicListByCode({
					DIC_CODE: "REASON_INVALIDATION"
				}).then((res) => {
					console.log(JSON.parse(res.datas));
					this.options = [JSON.parse(res.datas).datas]
				})
			}
		}
	}
</script>

<style>
	page {
		background-color: #F6F6F6;
	}
</style>
<style lang="scss" scoped>
	.shaixuan {
		width: 31rpx;
		height: 35rpx;
		margin-top: 16rpx;
	}


	.block {
		&_item {
			width: 750rpx;
			height: 123rpx;
			font-weight: 400;
			color: #000000;
			line-height: 45rpx;
			text-align: right;
			font-style: normal;
			text-align: center;
			line-height: 123rpx;
			border-bottom: 1rpx solid #F5F5F5;
		}

		.hr {
			height: 10rpx;
			background-color: #F5F5F5;
		}

		&_close {
			height: 123rpx;

			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 33rpx;
			color: #000000;
			text-align: center;
			line-height: 123rpx;
		}
	}

	.popup {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		top: 0;
		background: rgba(0, 0, 0, 0.5);
		display: flex;
		justify-content: center;
		align-items: baseline;
		z-index: 9999;

		.overlay {
			position: absolute;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
			z-index: 10001;
		}

		.content {
			background: #fff;
			width: 100%;
			margin-top: 90rpx;
			padding: 32rpx;
			z-index: 10002;

			&_time {
				&_title {
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 33rpx;
					color: #666666;
					text-align: left;
				}
			}

			&_times {

				&_left,
				&_right {
					width: 315rpx;
					height: 68rpx;
					background: #F6F8FB;
					border-radius: 6rpx;

					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 29rpx;
					color: #585756;
					line-height: 68rpx;
					text-align: center;
					font-style: normal;
				}

				&_con {
					height: 68rpx;
					line-height: 68rpx;
				}
			}

			&_box {
				height: 105rpx;
				background: #FFFFFF;
				margin-top: 20rpx;
				border-bottom: 1rpx solid #E6E6E6;
				line-height: 105rpx;

				&_left {
					width: 190rpx;
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 33rpx;
					color: #666666;
					text-align: left;

				}

				&_cont {
					flex: 1;
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 33rpx;
					color: #000000;
					text-align: left;
					padding: 0 9px;
				}

			}

			&_btn {
				margin-top: 31rpx;

				&_left {
					width: 321rpx;
					height: 86rpx;
					text-align: center;
					line-height: 86rpx;
					border: 1rpx dashed #e6e6e6;
				}

				&_right {
					width: 321rpx;
					height: 86rpx;
					text-align: center;
					line-height: 86rpx;
					background-color: #267DFF;
					color: #FFF;
				}
			}
		}
	}


	.search {

		/deep/.u-input {
			width: 688rpx;
			height: 65rpx;
			background: #FFFFFF;
			border: 1rpx solid #CCCCCC;
			margin: 0 auto;
			margin-top: 15rpx;
			border-radius: 32rpx;
		}

		&_btn {
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 29rpx;
			color: #666666;
			padding-left: 10rpx;
			border-left: 1rpx solid #E0E0E0;
		}
	}

	.box {
		width: 721rpx;
		background: #FFFFFF;
		margin: 0 auto;
		margin-top: 23rpx;
		border-radius: 15rpx;
		padding: 0 15rpx;
		padding-bottom: 33rpx;

		&_herder {
			padding: 30rpx 15rpx 25rpx 15rpx;
			border-bottom: 1rpx solid #F5F5F5;

			&_left {
				font-weight: 400;
				font-size: 33rpx;
				color: #1F1F1F;
				font-style: normal;
			}

			&_right {
				background: #FFDFDC;
				border-radius: 10rpx;
				border: 1rpx solid #E04734;
				padding: 14rpx 18rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 25rpx;
				color: #E04734;
			}

			&_right1 {
				background: #FFECDC;
				border-radius: 10rpx;
				border: 1rpx solid #E08A34;
				padding: 14rpx 18rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 25rpx;
				color: #E08A34;
			}
		}

		&_item {
			display: flex;
			align-items: center;
			margin-top: 30rpx;
			padding: 0 15rpx;

			&_btn {
				margin-top: 30rpx;
				border-top: 1rpx solid #F5F5F5;
				display: flex;
				justify-content: right;
				padding-top: 33rpx;
				padding-bottom: 23rpx;
				padding-right: 30rpx;

				&_left {
					width: 171rpx;
					height: 63rpx;
					background: #F6A234;
					border-radius: 31rpx;

					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 25rpx;
					color: #FFFFFF;
					line-height: 63rpx;
					text-align: center;
					margin-right: 38rpx;
				}

				&_right {
					width: 171rpx;
					height: 63rpx;
					background: #267DFF;
					border-radius: 31rpx;
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 25rpx;
					color: #FFFFFF;
					line-height: 63rpx;
					text-align: center;
					font-style: normal;
				}
			}
		}

		&_label {
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 29rpx;
			color: #999999;
			width: 175rpx;
			flex: 0 0 auto;
		}

		&_value {
			font-weight: 400;
			font-size: 29rpx;
			color: #1F1F1F;
		}
	}

	/deep/.placeholderClass {
		font-weight: 400;
		font-size: 33rpx;
		color: #CCCCCC;
	}

	.optionShow {
		width: 710rpx;
		padding: 30rpx;

		&_title {
			font-size: 30rpx;
			text-align: center;
			padding-bottom: 30rpx;
			border-bottom: 1rpx solid #CCCCCC;
			font-weight: 600;
		}

		&_box {
			padding: 30rpx 0;
			border-bottom: 1rpx solid #CCCCCC;
			font-size: 28rpx;
			padding-left: 15rpx;
		}

	}
</style>